<?php
class ModuleObject extends MasterObject{
	
	function ModuleObject( $config )
	{
		$this->MasterObject($config);
		$runCode = Load::moduleCode($this);
		$this->prize_arr= logic('lottery')->getPrizeArr();
		$this->$runCode();
		
	}
	function Main(){
		$prize_list = $this->getList();
		include handler('template')->file('lottery');
	}
	
	function choujiang(){
		  if (!X_IS_AJAX)
		{
			header('Location: /');
			exit;
		}  
		if(MEMBER_ID<1){
			$result['unlogin'] = 1;
			echo json_encode($result);
			exit;
			
		}else{
			$status = logic('lottery')->cj_status(MEMBER_ID);
			switch ($status) {
				case 0:
				$result['unlogin'] = 1;
				echo json_encode($result);
				exit;
				break;
				case 2;
				$result['zhongjiang'] = 1;
				echo json_encode($result);
				exit;
				break;
				case 3;
				$result['yichou'] = 1;
				echo json_encode($result);
				exit;
				break;
				default:
					;
				break;
			}
		}
		$prize_arr = $this->prize_arr;
		
		foreach ($prize_arr as $key => $val) {
			$arr[$val['id']] = $val['v'];
		}
		
		$rid = $this->getRand($arr); //根据概率获取奖项id
		
		$ary =  array(
				'uid'=>MEMBER_ID,
				'user'=>MEMBER_NAME,
				'prizeid'=>$rid,
				'time'=>time()
				
				);
		if(!logic('lottery')->add($ary)){
			$result['error'] = 1;
			echo json_encode($result);
			exit;
		}
		
		
		
		$res = $prize_arr[$rid]; //中奖项
		
		$min = $res['min'];
		$max = $res['max'];
		if($res['id']==0){ //七等奖
			$i = mt_rand(0,4);
			$result['angle'] = mt_rand($min[$i],$max[$i]);
		}else{
			$result['angle'] = mt_rand($min,$max); //随机生成一个角度
		}
		$result['prize'] = $res['prize'];
		$result['pid'] = $res['id'];
		echo json_encode($result);
	}
	
	function getList(){
		$prize_list = logic('lottery')->getList();
		return $prize_list;

	}
	
	function getRand($proArr) {
		$result = '';
	
		//概率数组的总概率精度
		$proSum = array_sum($proArr);
		
		//概率数组循环
		foreach ($proArr as $key => $proCur) {

			$randNum = mt_rand(1, $proSum);

			if ($randNum <= $proCur) {
				$result = $key;
				break;
			} else {
				$proSum -= $proCur;
			}
		}
		unset ($proArr);
	
		return $result;
	}
}